//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdHestonHullWhiteVanillaEngine.h"
using namespace Cephei::QL::Pricingengines::Vanilla;
#include <gen/QL/Models/Equity/HestonModel.h>
#include <gen/QL/Processes/HullWhiteProcess.h>
#include <gen/QL/Methods/Finitedifferences/Solvers/FdmSchemeDesc.h>
#include <gen/QL/PricingEngine.h>
using namespace Cephei::QL::Models::Equity;
using namespace Cephei::QL::Processes;
using namespace Cephei::QL::Methods::Finitedifferences::Solvers;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Vanilla::CFdHestonHullWhiteVanillaEngine::CFdHestonHullWhiteVanillaEngine (Cephei::QL::Models::Equity::IHestonModel^ model, Cephei::QL::Processes::IHullWhiteProcess^ hwProcess, Double corrEquityShortRate, Microsoft::FSharp::Core::FSharpOption<UInt64>^ tGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ xGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ vGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ rGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ dampingSteps, Microsoft::FSharp::Core::FSharpOption<Boolean>^ controlVariate, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>^ schemeDesc) : CPricingEngine(CFdHestonHullWhiteVanillaEngine::typeid)
{
    CHestonModel^ _Cmodel;
    CHullWhiteProcess^ _ChwProcess;
    CFdmSchemeDesc^ _CschemeDesc;
    try
    {
#ifdef HANDLE
        _phFdHestonHullWhiteVanillaEngine = NULL;
#endif
        _Cmodel = safe_cast<CHestonModel^> (model);
        _Cmodel->Lock();
        boost::shared_ptr<QuantLib::HestonModel>& _model = static_cast<boost::shared_ptr<QuantLib::HestonModel>&> (_Cmodel->GetShared ()); 
        _ChwProcess = safe_cast<CHullWhiteProcess^> (hwProcess);
        _ChwProcess->Lock();
        boost::shared_ptr<QuantLib::HullWhiteProcess>& _hwProcess = static_cast<boost::shared_ptr<QuantLib::HullWhiteProcess>&> (_ChwProcess->GetShared ()); 
        QuantLib::Real _corrEquityShortRate = (QuantLib::Real)ValueHelper::Convert (corrEquityShortRate); //d
        QuantLib::Size _tGrid = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (tGrid) ? (QuantLib::Size)ValueHelper::Convert (tGrid->Value) : 50); //4
        QuantLib::Size _xGrid = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (xGrid) ? (QuantLib::Size)ValueHelper::Convert (xGrid->Value) : 100); //4
        QuantLib::Size _vGrid = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (vGrid) ? (QuantLib::Size)ValueHelper::Convert (vGrid->Value) : 40); //4
        QuantLib::Size _rGrid = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (rGrid) ? (QuantLib::Size)ValueHelper::Convert (rGrid->Value) : 20); //4
        QuantLib::Size _dampingSteps = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (dampingSteps) ? (QuantLib::Size)ValueHelper::Convert (dampingSteps->Value) : 0); //4
        bool _controlVariate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (controlVariate) ? (bool)ValueHelper::Convert (controlVariate->Value) : true); //4
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>::IsSome::get (schemeDesc))
        {
            _CschemeDesc = safe_cast<CFdmSchemeDesc^> (schemeDesc->Value);
            _CschemeDesc->Lock();
        }
        QuantLib::FdmSchemeDesc& _schemeDesc = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>::IsSome::get (schemeDesc) ? static_cast<QuantLib::FdmSchemeDesc&> (_CschemeDesc->GetReference ()) : QuantLib::FdmSchemeDesc::Hundsdorfer()); //1
        _ppFdHestonHullWhiteVanillaEngine = new boost::shared_ptr<QuantLib::FdHestonHullWhiteVanillaEngine> (new QuantLib::FdHestonHullWhiteVanillaEngine ( _model,  _hwProcess,  _corrEquityShortRate,  _tGrid,  _xGrid,  _vGrid,  _rGrid,  _dampingSteps,  _controlVariate,  _schemeDesc ));
        SetPricingEngine (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdHestonHullWhiteVanillaEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmodel != nullptr) _Cmodel->Unlock();
        if (_ChwProcess != nullptr) _ChwProcess->Unlock();
        if (_CschemeDesc != nullptr) _CschemeDesc->Unlock();
    }
}
Cephei::QL::Pricingengines::Vanilla::CFdHestonHullWhiteVanillaEngine::CFdHestonHullWhiteVanillaEngine (boost::shared_ptr<QuantLib::FdHestonHullWhiteVanillaEngine>& childNative, Object^ owner) : CPricingEngine(CFdHestonHullWhiteVanillaEngine::typeid)
{
#ifdef HANDLE
	_phFdHestonHullWhiteVanillaEngine = NULL;
#endif
	_ppFdHestonHullWhiteVanillaEngine = &childNative;
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdHestonHullWhiteVanillaEngine));
}
Cephei::QL::Pricingengines::Vanilla::CFdHestonHullWhiteVanillaEngine::CFdHestonHullWhiteVanillaEngine (QuantLib::FdHestonHullWhiteVanillaEngine& childNative, Object^ owner) : CPricingEngine(CFdHestonHullWhiteVanillaEngine::typeid)
{
#ifdef HANDLE
	_phFdHestonHullWhiteVanillaEngine = NULL;
#endif
	_ppFdHestonHullWhiteVanillaEngine = new boost::shared_ptr<QuantLib::FdHestonHullWhiteVanillaEngine> (&childNative);
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdHestonHullWhiteVanillaEngine));
    _FdHestonHullWhiteVanillaEngineOwner = owner;
    _PricingEngineOwner = owner;
}

Cephei::QL::Pricingengines::Vanilla::CFdHestonHullWhiteVanillaEngine::CFdHestonHullWhiteVanillaEngine (CFdHestonHullWhiteVanillaEngine^ copy) : CPricingEngine(CFdHestonHullWhiteVanillaEngine::typeid)
{
#ifdef HANDLE
	_phFdHestonHullWhiteVanillaEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdHestonHullWhiteVanillaEngine = new boost::shared_ptr<QuantLib::FdHestonHullWhiteVanillaEngine> (copy->GetShared());
        _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdHestonHullWhiteVanillaEngine));
    }
}
Cephei::QL::Pricingengines::Vanilla::CFdHestonHullWhiteVanillaEngine::CFdHestonHullWhiteVanillaEngine (PLATFORM::Type^ t) : CPricingEngine(CFdHestonHullWhiteVanillaEngine::typeid)
{
#ifdef HANDLE
	_phFdHestonHullWhiteVanillaEngine = NULL;
#endif
	if (!t->IsSubclassOf(CFdHestonHullWhiteVanillaEngine::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Vanilla::CFdHestonHullWhiteVanillaEngine::CFdHestonHullWhiteVanillaEngine (QuantLib::Handle<QuantLib::FdHestonHullWhiteVanillaEngine>& childNative, Object^ owner)  : CPricingEngine(CFdHestonHullWhiteVanillaEngine::typeid)
{
	_phFdHestonHullWhiteVanillaEngine = &childNative;
	_ppFdHestonHullWhiteVanillaEngine = &static_cast<boost::shared_ptr<QuantLib::FdHestonHullWhiteVanillaEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdHestonHullWhiteVanillaEngine));
    _FdHestonHullWhiteVanillaEngineOwner = owner;
}
Cephei::QL::Pricingengines::Vanilla::CFdHestonHullWhiteVanillaEngine::CFdHestonHullWhiteVanillaEngine (QuantLib::Handle<QuantLib::FdHestonHullWhiteVanillaEngine> childNative)  : CPricingEngine(CFdHestonHullWhiteVanillaEngine::typeid)
{
	_phFdHestonHullWhiteVanillaEngine = &childNative;
	_ppFdHestonHullWhiteVanillaEngine = &static_cast<boost::shared_ptr<QuantLib::FdHestonHullWhiteVanillaEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdHestonHullWhiteVanillaEngine));
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Vanilla::CFdHestonHullWhiteVanillaEngine::CFdHestonHullWhiteVanillaEngine (QuantLib::FdHestonHullWhiteVanillaEngine childNative)  : CPricingEngine(CFdHestonHullWhiteVanillaEngine::typeid)
{
#ifdef HANDLE
	_phFdHestonHullWhiteVanillaEngine = NULL;
#endif
	_ppFdHestonHullWhiteVanillaEngine = new boost::shared_ptr<QuantLib::FdHestonHullWhiteVanillaEngine> (new QuantLib::FdHestonHullWhiteVanillaEngine (childNative));
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdHestonHullWhiteVanillaEngine));
}
#endif

Cephei::QL::Pricingengines::Vanilla::CFdHestonHullWhiteVanillaEngine::~CFdHestonHullWhiteVanillaEngine ()
{
    if (_ppFdHestonHullWhiteVanillaEngine != NULL)
    {
	    delete _ppFdHestonHullWhiteVanillaEngine;
        _ppFdHestonHullWhiteVanillaEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Vanilla::CFdHestonHullWhiteVanillaEngine::!CFdHestonHullWhiteVanillaEngine ()
{
    if (_ppFdHestonHullWhiteVanillaEngine != NULL)
    {
	    delete _ppFdHestonHullWhiteVanillaEngine;
    }
}
QuantLib::FdHestonHullWhiteVanillaEngine& Cephei::QL::Pricingengines::Vanilla::CFdHestonHullWhiteVanillaEngine::GetReference ()
{
    if (_ppFdHestonHullWhiteVanillaEngine == NULL) throw REFNEW NativeNullException ();
	return **_ppFdHestonHullWhiteVanillaEngine;
}
boost::shared_ptr<QuantLib::FdHestonHullWhiteVanillaEngine>& Cephei::QL::Pricingengines::Vanilla::CFdHestonHullWhiteVanillaEngine::GetShared ()
{
    if (_ppFdHestonHullWhiteVanillaEngine == NULL) throw REFNEW NativeNullException ();
	return *_ppFdHestonHullWhiteVanillaEngine;
}
QuantLib::FdHestonHullWhiteVanillaEngine* Cephei::QL::Pricingengines::Vanilla::CFdHestonHullWhiteVanillaEngine::GetPointer ()
{
    if (_ppFdHestonHullWhiteVanillaEngine == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdHestonHullWhiteVanillaEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdHestonHullWhiteVanillaEngine>& Cephei::QL::Pricingengines::Vanilla::CFdHestonHullWhiteVanillaEngine::GetHandle ()
{
	if (_phFdHestonHullWhiteVanillaEngine == NULL)
	{
		_phFdHestonHullWhiteVanillaEngine = new Handle<QuantLib::FdHestonHullWhiteVanillaEngine> (*_ppFdHestonHullWhiteVanillaEngine);
	}
	return *_phFdHestonHullWhiteVanillaEngine;
}
#endif
bool Cephei::QL::Pricingengines::Vanilla::CFdHestonHullWhiteVanillaEngine::HasNative () 
{
	return (_ppFdHestonHullWhiteVanillaEngine != NULL);
}

Cephei::QL::Pricingengines::Vanilla::IFdHestonHullWhiteVanillaEngine^ Cephei::QL::Pricingengines::Vanilla::CFdHestonHullWhiteVanillaEngine::Calculate::get ()
{
    try
    {
    	(*_ppFdHestonHullWhiteVanillaEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Pricingengines::Vanilla::IFdHestonHullWhiteVanillaEngine^ Cephei::QL::Pricingengines::Vanilla::CFdHestonHullWhiteVanillaEngine::EnableMultipleStrikesCaching (Cephei::Core::IVector<Double>^ strikes)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        strikes->Lock();
        INativeVector<Double>^ _NCIstrikes = strikes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCstrikes = safe_cast<CDoubleVector^>(_NCIstrikes);
        std::vector<QuantLib::Real>& _strikes = static_cast<std::vector<QuantLib::Real>&> (_NCstrikes->GetReference ());
    	(*_ppFdHestonHullWhiteVanillaEngine)->enableMultipleStrikesCaching ( _strikes );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (strikes != nullptr) strikes->Unlock(); //NO2  
    }
}
Cephei::QL::Pricingengines::Vanilla::IFdHestonHullWhiteVanillaEngine^ Cephei::QL::Pricingengines::Vanilla::CFdHestonHullWhiteVanillaEngine::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppFdHestonHullWhiteVanillaEngine)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Vanilla::IFdHestonHullWhiteVanillaEngine^ Cephei::QL::Pricingengines::Vanilla::CFdHestonHullWhiteVanillaEngine_Factory::Create (Cephei::QL::Models::Equity::IHestonModel^ model, Cephei::QL::Processes::IHullWhiteProcess^ hwProcess, Double corrEquityShortRate, Microsoft::FSharp::Core::FSharpOption<UInt64>^ tGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ xGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ vGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ rGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ dampingSteps, Microsoft::FSharp::Core::FSharpOption<Boolean>^ controlVariate, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>^ schemeDesc)
{
    return REFNEW CFdHestonHullWhiteVanillaEngine ( model,  hwProcess,  corrEquityShortRate,  tGrid,  xGrid,  vGrid,  rGrid,  dampingSteps,  controlVariate,  schemeDesc);
}
